﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MJM.CBD.Conn;
using MJM.SPP.EntidadesNegocio;

namespace MJM.SPP.AccesoDatos
{
    public class NodoAD
    {
        public List<NodoEN> Listar(int Opt,String Nombre)
        {
            List<NodoEN> lst = new List<NodoEN>();
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_s_T_NODPRO_Listar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmOpt, prmNombre;
            SqlDataReader dr;
            try
            {
                prmOpt = cmd.CreateParameter();
                prmOpt.ParameterName = "@vTipo";
                prmOpt.SqlDbType = SqlDbType.Int;
                prmOpt.Value = Opt;
                cmd.Parameters.Add(prmOpt);

                prmNombre = cmd.CreateParameter();
                prmNombre.ParameterName = "@vNombre";
                prmNombre.SqlDbType = SqlDbType.VarChar;
                prmNombre.Value = Nombre;
                cmd.Parameters.Add(prmNombre);

                cmd.Connection.Open();
                dr=cmd.ExecuteReader();
                while (dr.Read())
                {
                    NodoEN obj = new NodoEN();
                    obj.Codigo = dr.GetInt32(0);
                    obj.Nombre = dr.GetString(1);
                    obj.Padre = dr.GetString(2);
                    obj.IdPadre = dr.GetInt32(3);
                    lst.Add(obj);
                }
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
            }

            return lst;
        }

        public List<NodoEN> ListarPadres(int Opt)
        {
            List<NodoEN> lst = new List<NodoEN>();
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_s_T_PADPRO_Listar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmOpt;
            SqlDataReader dr;

            try
            {
                prmOpt = cmd.CreateParameter();
                prmOpt.ParameterName = "@vNivel";
                prmOpt.SqlDbType = SqlDbType.Int;
                prmOpt.Value = Opt;
                cmd.Parameters.Add(prmOpt);

                cmd.Connection.Open();
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    NodoEN obj = new NodoEN();
                    obj.Codigo = dr.GetInt32(0);
                    obj.Nombre = dr.GetString(1);
        /*            obj.Padre = dr.GetString(2);
                    obj.IdPadre = dr.GetInt32(3);*/
                    lst.Add(obj);
                }
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
            }

            return lst;
        }
    }
}
